package com.cheng.chengliving.commodity.controller;

import java.util.Arrays;
import java.util.Map;

import com.cheng.common.valid.SaveGroup;
import com.cheng.common.valid.UpdateGroup;
import com.cheng.common.valid.UpdateIsShowGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cheng.chengliving.commodity.entity.BrandEntity;
import com.cheng.chengliving.commodity.service.BrandService;
import com.cheng.common.utils.PageUtils;
import com.cheng.common.utils.R;



/**
 * 家具品牌
 *
 * @author zhucheng
 * @email 1920361386@qq.com
 * @date 2022-11-09 14:05:39
 */
@RestController
@RequestMapping("commodity/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("commodity:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("commodity:brand:info")
    public R info(@PathVariable("id") Long id){
		BrandEntity brand = brandService.getById(id);

        return R.ok().put("brand", brand);
    }

    /**
     * 1. @Validated 注解的作用就是启用对BrandEntity 字段进行校验;
     * 2. 如果没有该注解,则校验规则不生效;
     * 3. BindingResult result : SpringBoot 会将校验的错误放入到result
     * 4. 程序员可以通过BindingResult result 将校验得到的错误取出,然后进行相应处理
     * 5. 将数据校验异常交给全局异常处理器 ChenglivingExceptionControllerAdvice 处理,
     *       所以这里注销相关的校验代码
     * 6. @Validated({SaveGroup.class}) 表示调用save方法时进行参数校验,使用的是SaveGroup的校验规则
     * @param brand
     * @return
     */
    @RequestMapping("/save")
    //@RequiresPermissions("commodity:brand:save")
    public R save(@Validated({SaveGroup.class})@RequestBody BrandEntity brand /* , BindingResult result */){
        //先创建一个map,用于收集校验错误
//        HashMap<String, String> map = new HashMap <>();
//        if (result.hasErrors()){
//            //遍历result,将错误信息收集到map
//            result.getFieldErrors().forEach((item)->{
//                //先得到field
//                String field = item.getField();
//                //得到校验的错误信息
//                String message = item.getDefaultMessage();
//                map.put(field,message);
//            });
//            return R.error(400,"品牌表单数据不合法").put("data",map);
//        }else {
//            //没有校验错误,入库
            brandService.save(brand);

            return R.ok();

    }

    /**
     * 修改
     * 1. @Validated({UpdateGroup.class})表示进行修改/调用update方法,会进行参数校验,使用的是
     *    UpdateGroup.class的校验规则
     */
    @RequestMapping("/update")
    //@RequiresPermissions("commodity:brand:update")
    public R update(@Validated({UpdateGroup.class})@RequestBody BrandEntity brand){
		brandService.updateById(brand);

        return R.ok();
    }

    @RequestMapping("/update/isshow")
    //@RequiresPermissions("commodity:brand:update")
    public R updateIsShow(@Validated({UpdateIsShowGroup.class})@RequestBody BrandEntity brand){
        brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("commodity:brand:delete")
    public R delete(@RequestBody Long[] ids){
		brandService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/save2")
    public R save2(@Validated @RequestBody BrandEntity brand) {

        int num = 10/0;

        return R.ok();
    }

}
